package me.zhengjie.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import me.zhengjie.controller.WcsToSendWmsController;
import me.zhengjie.dao.*;
import me.zhengjie.enums.ConstantStatus;
import me.zhengjie.mapper.OutBoundTaskMapper;
import me.zhengjie.mapper.SortingMapper;
import me.zhengjie.server.*;
import me.zhengjie.vo.OutBoundTaskToWcsVo;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author xinglin.lu
* @description 针对表【iws_sorting(分拣表)】的数据库操作Service实现
* @createDate 2025-06-13 16:44:12
*/
@AllArgsConstructor
@Service
public class SortingServiceImpl extends ServiceImpl<SortingMapper, SortingDao>
    implements SortingService{
    private final SortingMapper sortingMapper;
    private final WcsToSendWmsController wcsToSendWmsController;
   private final MaterialService materialService;
    private final OutBoundTaskMapper outBoundTaskMapper;
//    private   final OutBoundTaskService outBoundTaskService;
    private final DispositionService dispositionService;
    @Override
    public String finishSorting(String outboundCode,String containerIdO, String containerIdT) throws IOException {
        SortingDao sortingDao = sortingMapper.selectOne(new LambdaQueryWrapper<SortingDao>()
                .eq(SortingDao::getContainerId, containerIdO)
                .eq(SortingDao::getOutboundCode, outboundCode));
        if (sortingDao.getFloor().equals("M2")) {
            wcsToSendWmsController.requiteWcs(outboundCode,containerIdO);
        }else {//正向
            String targetMaterialCode = sortingDao.getMaterialCode();
            List<MaterialDao> list = materialService.list(new LambdaQueryWrapper<MaterialDao>().eq(MaterialDao::getContainerCode, containerIdO));
            List<String> filteredMaterialCodes = list.stream()
                    .filter(e -> e.getMaterialCode().equals(targetMaterialCode)) // 过滤条件
                    .map(MaterialDao::getMaterialCode) // 提取 materialCode
                    .collect(Collectors.toList());
            String result = String.join(",", filteredMaterialCodes);
            OutBoundTaskToWcsVo wcsVo = new OutBoundTaskToWcsVo();
            wcsVo.setWmsTaskId(outboundCode);
            wcsVo.setContainerCode(containerIdO);
            //起点
            InetAddress ip = InetAddress.getLocalHost();
            DispositionDao dispositionDao = dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>()
                    .eq(DispositionDao::getIp, ip)
                    .eq(DispositionDao::getName,1));
            wcsVo.setSource(dispositionDao.getLocation());
            //终点
            wcsVo.setDestination(ConstantStatus.Wcs_Destination);
            OutBoundTaskDao outBoundTaskDao = outBoundTaskMapper.selectOne(new LambdaQueryWrapper<OutBoundTaskDao>().eq(OutBoundTaskDao::getCode, sortingDao.getOutboundTaskCode()));
            wcsVo.setPriority(outBoundTaskDao.getPriority());
            wcsVo.setFloor(sortingDao.getFloor());
            wcsVo.setDeliveryType(3);
            wcsVo.setTaskType("3");
            wcsVo.setMaterIalCode(result);
//            outBoundTaskService.sendToWcs(wcsVo);
            //反向
            if (containerIdO == null || containerIdT == null) {
                String targetMaterialCodere = sortingDao.getMaterialCode();
                List<MaterialDao> listre = materialService.list(new LambdaQueryWrapper<MaterialDao>().eq(MaterialDao::getContainerCode, containerIdO));
                List<String> filteredMaterialCodesre = listre.stream()
                        .filter(e -> e.getMaterialCode().equals(targetMaterialCodere)) // 过滤条件
                        .map(MaterialDao::getMaterialCode) // 提取 materialCode
                        .collect(Collectors.toList());
                String resultre = String.join(",", filteredMaterialCodesre);
                DispositionDao dispositionDaores = dispositionService.getOne(new LambdaQueryWrapper<DispositionDao>()
                        .eq(DispositionDao::getIp, ip)
                        .eq(DispositionDao::getName,2));
                OutBoundTaskToWcsVo reversewcsVo = new OutBoundTaskToWcsVo();
                reversewcsVo.setWmsTaskId(outboundCode);
                reversewcsVo.setContainerCode(containerIdT);
                //起点
                reversewcsVo.setSource(dispositionDaores.getLocation());
                //终点
                reversewcsVo.setDestination(ConstantStatus.Wcs_Destination);
                OutBoundTaskDao outBoundTaskDao1 = outBoundTaskMapper.selectOne(new LambdaQueryWrapper<OutBoundTaskDao>().eq(OutBoundTaskDao::getCode, sortingDao.getOutboundTaskCode()));
                reversewcsVo.setPriority(outBoundTaskDao1.getPriority());
                reversewcsVo.setFloor(sortingDao.getFloor());
                reversewcsVo.setDeliveryType(3);
                reversewcsVo.setTaskType("3");
                reversewcsVo.setMaterIalCode(resultre);
//                outBoundTaskService.sendToWcs(wcsVo);
            }
        }
        return "分拣结束";
    }

    @Override
    public String sortingToWcs(String outBoundOrderCode) {
        return "sucess";
    }
}




